package de.tud.binpacking;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import de.tud.binpacking.data.Batch;
import de.tud.binpacking.data.Solution;
import de.tud.binpacking.heuristic.construction.AStartHeuristic;
import de.tud.binpacking.heuristic.construction.ForEachOrderOneBatch;
import de.tud.binpacking.heuristic.construction.NearestNeighborAisles;
import de.tud.binpacking.heuristic.construction.NearestNeighborBS;
import de.tud.binpacking.heuristic.construction.NearestNeighborSize;
import de.tud.binpacking.heuristic.improvement.AImprovementHeuristic;
import de.tud.binpacking.heuristic.improvement.LocalSearchBestImprovement;
import de.tud.binpacking.instance.Importer;
import de.tud.binpacking.instance.Instance;

public class MainTest {

	public static void main(String[] args) {
		List<Instance> instances;
		try {
			instances = Importer.importInstances(30, 30, "C:\\Temp\\Logistiksysteme\\eclipse\\workspace\\BinPacking\\instances");


			int i = 0, ac = 0, bc = 0, cc = 0, dc = 0;
			for(Instance instance : instances){
				System.out.println("\nSzenario " + i++);
				AStartHeuristic a = new NearestNeighborBS(instance);
				AStartHeuristic b = new NearestNeighborAisles(instance);
				AStartHeuristic c = new NearestNeighborSize(instance);
				AStartHeuristic d = new ForEachOrderOneBatch(instance);

				a.run();
				System.out.print("a: " + a.getSolution().getCosts() + "\t" + a.getSolution().getNoBatches());
				AImprovementHeuristic ia = new LocalSearchBestImprovement(instance);
				Solution sol1 = ia.improveSolution(a.getSolution());
				System.out.println("\t a imp:\t" + sol1.getCosts() + "\t" + sol1.getNoBatches());
				printFreeCapacity(sol1);
				
				b.run();
				System.out.print("b: " + b.getSolution().getCosts() + "\t" + b.getSolution().getNoBatches());
				AImprovementHeuristic ib = new LocalSearchBestImprovement(instance);
				Solution sol2 = ib.improveSolution(b.getSolution());
				System.out.println("\t b imp:\t" + sol2.getCosts() + "\t" + sol2.getNoBatches());
				printFreeCapacity(sol2);
				
				c.run();
				System.out.print("c: " + c.getSolution().getCosts() + "\t" + c.getSolution().getNoBatches());
				AImprovementHeuristic ic = new LocalSearchBestImprovement(instance);
				Solution sol3 = ic.improveSolution(c.getSolution());
				System.out.println("\t c imp:\t" + sol3.getCosts() + "\t" + sol3.getNoBatches());
				printFreeCapacity(sol3);

				
				d.run();
				System.out.print("d: " + d.getSolution().getCosts() + "\t" + d.getSolution().getNoBatches());
				AImprovementHeuristic id = new LocalSearchBestImprovement(instance);
				Solution sol4 = id.improveSolution(d.getSolution());
				System.out.println("\t d imp:\t" + sol4.getCosts() + "\t" + sol4.getNoBatches());

				ac += a.getSolution().getCosts();
				bc += b.getSolution().getCosts();
				cc += c.getSolution().getCosts();
				dc += d.getSolution().getCosts();
			}

			System.out.println("a: " + ac/40);
			System.out.println("b: " + bc/40);
			System.out.println("c: " + cc/40);
			System.out.println("d: " + dc/40);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void printFreeCapacity(Solution solution){
		for(Batch batch : solution.getBatches()){
			System.out.print(batch.getFreeCapacity() + " ");
		}
		System.out.println();
	}

}
